(function(pageName){
  sugarcane.module[pageName] = function(){
    $('.j-pagescreen').addClass('fn-hide');
    var urlParams = sugarcane.getUrlParams(),
      docType = 1,
      newsId = urlParams['newsId'],
      id = urlParams['id'];
      if (id == '0'){
        id = newsId;
      }
    if ($('#j-screen-'+pageName+id).length) {
      var pageScreen = $('#j-screen-'+pageName+id);
      pageScreen.removeClass('fn-hide');
      var _scrollTop = pageScreen.data('scrolltop') || 0;
      setTimeout(function(){
        $(window).scrollTop(_scrollTop);
      }, 10);
      return false;
    }
    var pageScreen = $('<div class="j-pagescreen j-pagescreen-detail" id="j-screen-'+pageName+id+'"></div>');
    $('body').append(pageScreen);

    detailShow();
    // 获取新闻
    var detailData = {};
    function detailShow(){
      if (id) {
        sugarcane.ajaxApi({
          "url": "/article/getArticleDetail",
          'type': 'post',
          "data": {"id": +id},
          "callback": function(res){
            if (res['code'] == 0) {
              detailData = res['data'];
              if(res.data.docType){
                docType = res.data.docType;
              }
              //document.title = detailData['title'];
              sugarcane.twoShare({
                "id": res.data.id,
                "title": res.data.title,
                "desc": '分享来自经济日报客户端，更多精彩请下载http://d.jingjiribao.cn/index.html',
                "imgUrl": res.data.shareImage,
                "link": res.data.shareUrl,
                "docType": res.data.docType
              });
              // 标题
              pageScreen.append(newGrapes["newstitle"].init({"title":detailData['title']}));
              var contentTpl = $('<div class="tag-content"></div>')
              var dataArr = [
                {
                  'text': '稿件标题',
                  'key': 'title'
                },{
                  'text': '首页标题',
                  'key': 'homeTitle'
                },{
                  'text': '副标题',
                  'key': 'subTitle'
                },{
                  'text': '类别',
                  'key': 'tagTypeNameList'
                },,{
                  'text': '所在栏目',
                  'key': 'channelName'
                },{
                  'text': '引用栏目',
                  'key': 'quoteChannelNameList'
                },{
                  'text': '来源',
                  'key': 'source'
                },{
                  'text': '作者',
                  'key': 'author'
                },{
                  'text': '关键词',
                  'key': 'keyword'
                },{
                  'text': '摘要',
                  'key': 'abstractContent'
                },{
                  'text': '敏感词',
                  'key': 'abstractContent'
                },{
                  'text': '是否原创',
                  'key': 'original'
                },{
                  'text': '稿件内容',
                  'key': 'content'
                }
              ];
              dataArr.forEach(function(item){
                var _content = res.data[item['key']] || '';
                if(item['key'] == 'original'){
                  if(res.data[item['key']]){
                    _content = '是';
                  }else{
                    _content = '否';
                  }

                }
                var _part = `<div class="part">
                  <p class="title">`+item.text+`</p>
                  <p class="content">`+ _content+`</p>
                </div>`;
                contentTpl.append(_part);
              })
              pageScreen.append(contentTpl);
              pageScreen.find('*').each(function(){
                var _fontSize = this.style.fontSize,
                    _lineHeight = this.style.lineHeight;
                  if (_fontSize) {
                    _fontSize = _fontSize.toLowerCase();
                    if (_fontSize.indexOf('px') > -1) {
                      _fontSize = (_fontSize.replace('px', '') * pageScreen.width() / 375) + 'px';
                      $(this).css('font-size', '0.427rem');
                    }else{
                      $(this).css('font-size', '0.427rem');
                    }
                  }else{
                    $(this).css("font-size", "0.427rem");
                    // $(this).addClass('app-fontsize-' + sugarcane['app']['fontSize']);
                  }
                  if (_lineHeight) {
                    _lineHeight = _lineHeight.toLowerCase();
                    if (_lineHeight.indexOf('px') > -1) {
                      _lineHeight = (_lineHeight.replace('px', '') * pageScreen.width() / 375) + 'px';
                      $(this).css('line-height', '0.587rem');
                    }
                  }else{
                    $(this).css('line-height',"0.587rem");
                  }
              });
            } else {
              showerrorView();
              if(!sugarcane['app']['isShare']){
                peach.hiddenAppbar();
              }
            
            }
          }
        });
      }
    }
   
   

    function showerrorView(){
      var clientHeight = document.documentElement.clientHeight;
      pageScreen.css('position','relative').css('height',clientHeight+'px');
      var errorviewObj = $(`<div class="app-errorview">
          <img class="img" src="/static/img/isempty.png">
        <p class="error-tip">暂时没有数据</p>
      </div>`);
       pageScreen.append(errorviewObj);
    }
  }

  sugarcane.module['state'][pageName] = 'load';
  sugarcane.module.callback(pageName);
}('newsdetail'));
